
#ifndef _MEMBER_BSCRIPT_H_
#define _MEMBER_BSCRIPT_H_

#include "IStack.h"
#include "member_BSource.h"

#ifdef linux
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#endif

#ifdef _WIN32
#include <Windows.h>
#endif

#define script_member(script)	((member_BScript*)((BScript*)script)->_ptr)

namespace BWE
{
	struct CallInfo : public BThing
	{
		int			pos = -1;
		int			top = -1;
		BClass*		self = 0;
		BString		type;
	};

	class member_BScript
	{
		friend class BScript;
	public:
		static BMap<BString, BClassHolder>		basicClassMap;

	public:
		member_BScript(BScript* script);
		~member_BScript();

		ILibrary* importLibrary(const BString& libname);
		const BString& cacheFile(const BString& fileName);
		bool createValue(BString& word, BValue& value);

		BExp* cloneExp(BExp* exp) const;
		BString& errorMessage(int index);
		BString& errorMessage(BExp* exp = 0);
		void sourceMessage(const BValue& value);

		void combine();
		void serialize();
		void serialize(BExp* exp);
		void link();

		void debug(BExp* exp);
		void debugResume();
		void routine();

		int perform(BExp* exp);
		void backStack(int pos);
		BValue* variant(const BString& name);
		BValue* findVariant(const BString& name);
		FuncInfo* findFunction(const BString& name);
		BClass* createTempClass(const BString& type);

		bool locateVariant(BValue* vptr, BString& type, BString& name);

	public:
		BScript*				boss;
		BString					path;
		BArray<BSourceHolder>	sources;
		bool					debugging;
		BSet<BString>			breakpointSet;
		BSet<BString>			includePaths;
		BSet<BString>			importPaths;
		BMap<BString, BString>	cachedFileMap;

		BArray<VariantInfo>				presetVariants;
		BMap<BString, BEnumHolder>		presetEnumMap;
		BMap<BString, BClassHolder>		presetClassMap;
		BMap<BString, FuncInfoHolder>	presetFunctionMap;

		bool					error;
		BValue					message;
		bool					unlinked;
		bool					running;
		int						counter;
		bool					breaked;
		BString					command;
		int						global;
		int						current;
		IStack<BValue>			stack;
		BMap<int, BString>		nameMap;
		BMap<int, BString>		typeMap;
		BArray<BExpHolder>		sequence;
		IStack<BExpArray>		loopStack;

		BArray<BExpHolder>				inclibs;
		BMap<BString, BExpHolder>		declMap;
		BMap<BString, BEnumHolder>		enumMap;
		BMap<BString, BClassHolder>		classMap;
		BMap<BString, FuncInfoHolder>	functionMap;

#ifdef linux
		pthread_t			paused_thread_id;
		pthread_cond_t 		thread_cond;
		pthread_mutex_t		thread_mutex;
#endif

#ifdef _WIN32
		DWORD			paused_thread_id;
		HANDLE			paused_handle;
#endif
	};

}

#endif